package com.topscomm.file.convert.aspose;

import java.io.FileOutputStream;

import com.topscomm.core.FileFormatEnum;
import com.topscomm.file.util.FileUtil;

/**
 * 转pdf文件
 * 
 * @description:
 * @author: donghang
 * @date: 2021年3月15日下午7:27:04
 * @modify:
 */
public class AsposeFileConvertToPdf {
	/**
	 * 转换文件
	 * 
	 * @description:
	 * @param filePath
	 * @param targetPath
	 * @return
	 * @author: donghang
	 * @throws Exception
	 * @date: 2021年3月15日下午7:26:46
	 * @modify:
	 */
	public static String fileToPdf(String identifier, String filePath, String targetPath) throws Exception {
		String mimeType = FileUtil.getMimeType(filePath);
		if (FileUtil.matchMimeType(mimeType, FileFormatEnum.WORD.mimeType)) {
			return wordToPdf(identifier, filePath, targetPath);
		}
		if (FileUtil.matchMimeType(mimeType, FileFormatEnum.EXCEL.mimeType)) {
			return excelToPdf(identifier, filePath, targetPath);
		}
		if (FileUtil.matchMimeType(mimeType, FileFormatEnum.PPT.mimeType)) {
			return pptToPdf(identifier, filePath, targetPath);
		}
		return null;
	}

	/**
	 * word转pdf
	 * 
	 * @description:
	 * @param filePath
	 * @param targetPath
	 * @return
	 * @author: donghang
	 * @throws Exception
	 * @date: 2021年3月15日下午7:01:20
	 * @modify:
	 */
	private static String wordToPdf(String identifier, String filePath, String targetPath) throws Exception {
		AsposeSetLicense.setWordAsposeLicense();

		String fileName = targetPath.substring(targetPath.lastIndexOf("/") + 1);
		String pdfFilePath = targetPath + "/" + fileName + ".pdf";
		FileOutputStream fileOS = new FileOutputStream(pdfFilePath);
		com.aspose.words.Document doc = new com.aspose.words.Document(filePath);
		com.aspose.words.PdfSaveOptions pdfSaveOptions = new com.aspose.words.PdfSaveOptions();
		doc.save(fileOS, pdfSaveOptions);
		if (fileOS != null) {
			fileOS.flush();
			fileOS.close();
		}

		return pdfFilePath;
	}

	/**
	 * excel转pdf
	 * 
	 * @description:
	 * @param filePath
	 * @param targetPath
	 * @return
	 * @author: donghang
	 * @throws Exception
	 * @date: 2021年3月15日下午7:01:41
	 * @modify:
	 */
	private static String excelToPdf(String identifier, String filePath, String targetPath) throws Exception {
		AsposeSetLicense.setExcelAsposeLicense();

		String fileName = targetPath.substring(targetPath.lastIndexOf("/") + 1);
		String pdfFilePath = targetPath + "/" + fileName + ".pdf";
		FileOutputStream fileOS = new FileOutputStream(pdfFilePath);
		com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(filePath);
		com.aspose.cells.PdfSaveOptions pdfSaveOptions = new com.aspose.cells.PdfSaveOptions();
		pdfSaveOptions.setOnePagePerSheet(false);
		pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
		wb.save(fileOS, pdfSaveOptions);
		if (fileOS != null) {
			fileOS.flush();
			fileOS.close();
		}

		return pdfFilePath;
	}

	/**
	 * @description: ppt转pdf
	 * @param filePath
	 * @param targetPath
	 * @return
	 * @throws Exception
	 * @author: wanghongwei
	 * @date: 2021-3-19 11:27:04
	 * @modify:
	 */
	private static String pptToPdf(String identifier, String filePath, String targetPath) throws Exception {
		// AsposeSetLicense.setPowerPointAsposeLicense();
		String fileName = targetPath.substring(targetPath.lastIndexOf("/") + 1);
		String pdfFilePath = targetPath + "/" + fileName + ".pdf";
		com.aspose.slides.Presentation presentation = new com.aspose.slides.Presentation(filePath);
		presentation.save(pdfFilePath, com.aspose.slides.SaveFormat.Pdf);
		return pdfFilePath;
	}
}
